/*
   Copyright (c) 2011, 2014, 2015 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-04 Marco Riccardi - initial implementation
 *  2014-12-20 Marco Riccardi - resource leak
 *  2015-01-11 Marco Riccardi - bug
 *
 */
package org.mizartools.system;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public final class EnvironFile {

	private static String MIZFILES;
	private static String MIZAR_RELEASE_NBR = "";
	private static String MIZAR_VERSION_NBR = "";
	private static String MIZAR_VARIANT_NBR = "";
	private static int NUMBER_OF_ARTICLES = 0;
	private static String MML_VERSION = "";

	static {
		MIZFILES = System.getenv("MIZFILES");
		if (MIZFILES != null){
			File file = new File(MIZFILES, "mml.ini");
			try {
				FileReader fileReader = new FileReader(file);
				try {
					LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
					String inputLine;
					String key;
					String value;
					int index;
					while ((inputLine = lineNumberReader.readLine()) != null){
						index = inputLine.indexOf("="); 
						if (index >= 0){
							key = inputLine.substring(0, index);
							value = inputLine.substring(index+1);
							if (key.equals("MizarReleaseNbr")){
								MIZAR_RELEASE_NBR = value;
							}
							if (key.equals("MizarVersionNbr")){
								MIZAR_VERSION_NBR = value;
							}
							if (key.equals("MizarVariantNbr")){
								MIZAR_VARIANT_NBR = value;
							}
							if (key.equals("NumberOfArticles")){
								NUMBER_OF_ARTICLES = Integer.parseInt(value);
							}
							if (key.equals("MMLVersion")){
								MML_VERSION = value;
							}
						};
					}
					lineNumberReader.close();
				}
				catch (IOException e) {}
				finally{
					try {
						fileReader.close();
						} 
					catch (IOException e) {}
				}
			} catch (FileNotFoundException e) {}
		} 
	}
	
	private EnvironFile(){
	}
	
	public static String getMIZFILES() {
		return MIZFILES;
	}

	public static String getMizarReleaseNbr(){
		return MIZAR_RELEASE_NBR;
	}

	public static String getMizarVersionNbr(){
		return MIZAR_VERSION_NBR;
	}

	public static String getMizarVariantNbr(){
		return MIZAR_VARIANT_NBR;
	}

	public static int getNumberOfArticles(){
		return NUMBER_OF_ARTICLES;
	}

	public static String getMMLVersion(){
		return MML_VERSION;
	}

}
